package com.ux.manager
{
	import com.greensock.TweenMax;
	import com.ux.util.AlphaControlUtil;
	import com.ux.util.JsUtil;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	
	/**
	 * @author winnie
	 */	
	public class StageManager 
	{
		
		/**
		 * document class 의 stage 
		 */		
		public static var stage : Stage;
		
		private static var dimData : BitmapData;
		private static var dim : Sprite;
		private static var _loadingClip : MovieClip; 
		
		private static var _bufferDimData:BitmapData;
		private static var _bufferDim:Sprite;
		
		
		
		public static function get loadingClip():MovieClip
		{
			return _loadingClip;
		}
		
		public static function set loadingClip(value:MovieClip):void
		{
			_loadingClip = value;
		}
		
		public static function get dimX():Number{
			return dim.x;
		}
		public static function set dimX(value:Number):void{
			dim.x = value;
		}
		public static function get dimY():Number{
			return dim.y;
		}
		public static function set dimY(value:Number):void{
			dim.y = value;
		}
		public static function get dimWidth():Number{
			return dim.width;
		}
		public static function set dimWidth(value:Number):void{
			dim.width = value;
		}
		public static function get dimHeight():Number{
			return dim.height;
		}
		public static function set dimHeight(value:Number):void{
			dim.height = value;
		}
		
		
		/** _showDuration : 나타나는 시간. (alpha 로 서서히 나타남 ) **/
		public static function showDim( _duration : Number = 0.3 ):void
		{
			if( stage == null )
			{
				trace(" ============== KJH TRACE ==============> StageManager. stage를 등록 해 주세요 " );
			}
			else
			{
				if( dim == null )
				{
					setDim(0xffffff, 0.2 );
				}
				
				dim.alpha = 0;
				stage.addChild( dim );
				AlphaControlUtil.changeAlpha( dim, _duration, 1 );
			}
		}
		
		
		
		
		/** _duration : 사라지는 시간. (alpha 로 서서히 나타남 ) **/
		public static function closeDim( _duration : Number = 0.3 ):void
		{
			if( stage == null )
			{
				trace(" ==> StageManager. closeDim : stage를 등록 해 주세요 " );
			}
			else
			{
				if( dim != null )
				{
					AlphaControlUtil.changeAlpha( dim, _duration, 0, onCloseComplete );
				}
			}
		}
		
		public static function onCloseComplete():void{
			if(dim != null && stage.contains( dim ) ) stage.removeChild( dim );
		}
		
		public static function onBufferOffComplete():void{
			if( _bufferDim != null && stage.contains( _bufferDim ) ) stage.removeChild( _bufferDim );
		}
		
		
		public static function fadeOutComplete():void
		{
			stage.removeChild( dim );
		}
		
		
		
		/** _clolr: color ( ex : 0xFFFFFF ) , _alpha : alpha ( 0 ~ 1 )  **/
		public static function setDim(_color:uint, _alpha:Number ):void 
		{
			if( dimData )
			{
				dimData.dispose();
				dimData = null;
			}
			
			dimData = new BitmapData( stage.stageWidth, stage.stageHeight, false, _color );
			
			var bit:Bitmap = new Bitmap( dimData );
			bit.alpha = _alpha;
			dim = new Sprite(); 
			dim.addChild( bit );
			
			stage.addEventListener( Event.RESIZE, onResize ); 
		}
		
		/** _clolr: color ( ex : 0xFFFFFF ) , _alpha : alpha ( 0 ~ 1 )  **/
		public static function setBufferDim(_color:uint, _alpha:Number ):void 
		{
			if( _bufferDimData )
			{
				_bufferDimData.dispose();
				_bufferDimData = null;
			}
			
			_bufferDimData = new BitmapData( stage.stageWidth, stage.stageHeight, false, _color );
			
			var bit:Bitmap = new Bitmap( _bufferDimData );
			bit.alpha = _alpha;
			_bufferDim = new Sprite(); 
			_bufferDim.addChild( bit );
			
			stage.addEventListener( Event.RESIZE, onResize ); 
		}
		
		private static function onResize(event:Event):void{
			dim.width = stage.stageWidth;
			//			dim.height = stage.stageHeight;
		}
		
		
		public static function bufferOn():void
		{
			if( _loadingClip )
			{
				if( _bufferDim == null ) setBufferDim(0xffffff, 0.2 );
				
				_bufferDim.alpha = 0;
				stage.addChild( _bufferDim );
				AlphaControlUtil.changeAlpha( _bufferDim, 0.2, 1 );
				
				if( stage.contains( _loadingClip ) == false ) stage.addChild( _loadingClip );
				
				_loadingClip.alpha = 0;
				_loadingClip.visible = true;
				
				stage.setChildIndex( _loadingClip, stage.numChildren-1 );
				
				_loadingClip.x = ( stage.stageWidth - _loadingClip.width ) /2;
				_loadingClip.y = ( stage.stageHeight- _loadingClip.height ) /2;
				
				TweenMax.killTweensOf( _loadingClip );
				TweenMax.to( _loadingClip, 0.5, {autoAlpha:1} );
			}
		}
		
		
		public static function bufferOff():void
		{
			if( _loadingClip )
			{
				if( stage.contains( _loadingClip ) == false ) stage.addChild( _loadingClip );
				
				TweenMax.killTweensOf( _loadingClip );
				TweenMax.to( _loadingClip, 0.5, {autoAlpha:0, onComplete:onBufferOff} );
				
				if( _bufferDim != null ) AlphaControlUtil.changeAlpha( _bufferDim, 0.2, 0, onBufferOffComplete );
			}
		}
		
		private static function onBufferOff():void
		{
			if( stage.contains( _loadingClip ) == true ) stage.removeChild( _loadingClip );
		}
		
		
		
		/**
		 *  StageManager constructor
		 */		
		public function StageManager()
		{
			
		}
	}
}